from rest_framework.response import Response


class APIResponse(Response):
    def __init__(self, code=200, msg=None, data=None, status=None,
                 template_name=None, headers=None,
                 exception=False, content_type=None, **kwargs):
        dic = {'code': code, 'msg': msg}
        if data:
            dic['data'] = data
        else:
            dic['data'] = None
        if kwargs:
            dic.update(kwargs)

        super().__init__(data=dic, status=status,
                         template_name=template_name, headers=headers,
                         exception=exception, content_type=content_type)

    @staticmethod
    def success(data=None, message="成功", **kwargs):
        return APIResponse(code=200, msg=message, data=data, **kwargs)

    @staticmethod
    def bad_request(data=None, message="请求错误", **kwargs):
        return APIResponse(code=400, msg=message, data=data, **kwargs)

    @staticmethod
    def unauthorized(data=None, message="未授权", **kwargs):
        return APIResponse(code=401, msg=message, data=data, **kwargs)

    @staticmethod
    def not_found(data=None, message="资源未找到", **kwargs):
        return APIResponse(code=404, msg=message, data=data, **kwargs)

    @staticmethod
    def error(data=None, message="内部错误", **kwargs):
        return APIResponse(code=500, msg=message, data=data, **kwargs)

    @staticmethod
    def unknown(data=None, message="未知错误", **kwargs):
        return APIResponse(code=500, msg=message, data=data, **kwargs)